##########################################################################################

library(optparse)
library(Seurat)

##########################################################################################
option_list <- list(
    make_option(c("--input_file"), type = "character"),
    make_option(c("--cell_type_file"), type = "character"),
    make_option(c("--out_path"), type = "character") 
)

if(1!=1){
    
    ## 单细胞表达文件
    input_file <- "~/20231121_singleMuti/input/testis_combined.Rdata"

    ## 通过质控的细胞
    cell_type_file <- "~/20231121_singleMuti/results/atac_res/testis_merge_all_qc_barcode_new.txt"

    ## 输出
    out_path <- "~/20231121_singleMuti/results/monocole"

}

###########################################################################################
parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

input_file <- opt$input_file
cell_type_file <- opt$cell_type_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

###########################################################################################

a <- load(input_file)
cell_type <- read.csv(cell_type_file)
cell_type$V1 <- gsub( "#" , "_" , cell_type$V1 )

###########################################################################################

scrnat <- testis_combined
scrnat$cell_type <- ifelse(scrnat$seurat_clusters==0,"Myoid cells",
  ifelse(scrnat$seurat_clusters==1,"Round&ElongateS.tids",
  ifelse(scrnat$seurat_clusters==2,"Sperm",
  ifelse(scrnat$seurat_clusters==3,"Leydig cells",
  ifelse(scrnat$seurat_clusters==4,"Differenting&Differented SPG",
  ifelse(scrnat$seurat_clusters==5,"SSC",
  ifelse(scrnat$seurat_clusters==6,"Endothelial cells",
  ifelse(scrnat$seurat_clusters==7,"Early stage of spermatids",
  ifelse(scrnat$seurat_clusters==8,"Leptotene",
  ifelse(scrnat$seurat_clusters==9,"Zygotene",
  ifelse(scrnat$seurat_clusters==10,"Diplotene",
  ifelse(scrnat$seurat_clusters==11,"Sertoli cells",
  ifelse(scrnat$seurat_clusters==12,"Patchytene",
  ifelse(scrnat$seurat_clusters==13,"Patchytene",
  ifelse(scrnat$seurat_clusters==14,"Macrophages",
  ifelse(scrnat$seurat_clusters==15,"Pericytes",
  ifelse(scrnat$seurat_clusters==16,"NKT cells","unkown")))))))))))))))))

scrnat$seurat_clusters <- as.character(scrnat$seurat_clusters)
scrnat$seurat_clusters <- ifelse( scrnat$seurat_clusters %in% c(12,13) , 17 , scrnat$seurat_clusters)
scrnat$seurat_clusters <- as.numeric( scrnat$seurat_clusters )
scrnat$seurat_clusters <- factor(scrnat$seurat_clusters)

###########################################################################################
## 满足atac质控的细胞
scrnat <- subset(scrnat , cell %in% cell_type$V1)

###########################################################################################

out_file <- paste0( out_path , "/testis_combined.annotationCellType.Rdata" ) 
save( scrnat, file = out_file )